/************************************************************************
This file is part of Polar4linux
Copyright (C) 2010-2011 Eric jrdn2
Portions Copyright (C) 2007 Matti Juvonen <mpj@iki.fi> (RS400 Tools)
Portions Copyright (C) 2006 Jani Hursti <jani.hursti@hut.fi>,
Portions Copyright (C) 2001-2003 Tom Oliveira e Silva

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
************************************************************************/


#ifndef _RS400_UTIL_H
#define _RS400_UTIL_H
#include "RS400_struct.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>

void RS400_reset_session(rs400_session *session);

int rs400_decode_string(unsigned char result[], const unsigned char rawdata[]);
int rs400_decode_string(unsigned char result[], const unsigned char rawdata[], unsigned int maxsize);
int rs400_decode_stringDebug(unsigned char result[], const unsigned char rawdata[], unsigned int maxsize);

int rs400_encode_string(unsigned char result[], const unsigned char string[]);
int rs400_encode_string(unsigned char result[], const unsigned char string[], unsigned int maxsize);

void displaybuf(unsigned char *buf,unsigned int size);


unsigned int bcd2i(int bcd);

double hms2sec(unsigned char h, unsigned char m, unsigned char s);
double hmst2sec(unsigned char h, unsigned char m, unsigned char s, unsigned char t);
double hmsc2sec(unsigned char h, unsigned char m, unsigned char s, unsigned char c);
double bcd_hms2sec(const unsigned char pos[]);
double bcd_hmsc2sec(const unsigned char pos[]);
double short2sec(const unsigned char pos[]);
unsigned short toshort(const unsigned char pos[]);
unsigned int pace(unsigned char pos[]);
void timetostr(char str[], double time);
void hmstostr(char str[], int h,int m, int n,char sep);

int msb_lsb_to_decimal(int msb, int lsb);
int msb_lsb_to_weighKG(int msb, int lsb);
int msb_lsb_to_weighLB(int msb, int lsb);
void int_to_msb_lsb(int dec, unsigned char* msb, unsigned char* lsb);
void weighLB_to_msb_lsb(int dec, unsigned char* msb, unsigned char* lsb);
void weighKG_to_msb_lsb(int dec, unsigned char* msb, unsigned char* lsb);
int hexa_to_same_decimal(int hex); //if hex=0x42, then return is 42 (decimal base)
int decimal_to_same_hexa(int decimal); //if decimal = 42, then return is 0x42 (hexa base)
void hex2ddmmyy(unsigned char hex0,unsigned char hex1,unsigned char *dd,unsigned char *mm, unsigned char *yy);
void ddmmyy2hex(unsigned char dd,unsigned char mm, unsigned char yy,unsigned char *hex0,unsigned char *hex1);

float compute_Kcal(float PCfc,int weight,int vo2);
void _initialize_pdd(rs400_pdd_content &info, int day, int month, int year);
void compute_laptimes(rs400_session &session);
#endif
